import SparkApi
import time

appid = "0f5b347d"     # 替换为你的 APPID
api_secret = "ODAxODljMGM5NGE5OTYyMzZiODM3ZjM2"  # 替换为你的 APISecret
api_key = "033cca0b7eb49581a0ee542a937de879"     # 替换为你的 APIKey
domain = "4.0Ultra"
Spark_url = "wss://spark-api.xf-yun.com/v4.0/chat"

# 初始上下文内容
text = []

def getText(role, content):
    jsoncon = {"role": role, "content": content}
    text.append(jsoncon)
    return text

def getlength(text):
    length = sum(len(content["content"]) for content in text)
    return length

def checklen(text):
    while getlength(text) > 8000:
        del text[0]
    return text

def get_response(input_text):
    question = checklen(getText("user", input_text))
    SparkApi.answer = ""
    SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)
    response = SparkApi.answer
    getText("assistant", response)
    return response
